package homework.three;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ThreeDemo {

    public static String className = "homework.three.Person";
    public static String fileName = "D:\\test.txt";
    public static String content = null;

    public static void createObj() throws Exception {
        //Person person = new Person();

        Class person = Class.forName(className);
        Object obj = person.newInstance();
        System.out.println(obj instanceof Person);
    }

    public static void fieldOpr() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class clazz = Class.forName(className);
        Constructor constructor = clazz.getConstructor();
        Object person = constructor.newInstance();
        Field field0 = clazz.getDeclaredField("idNo");// 私有属性
        field0.setAccessible(true);
        field0.set(person, "5122245566");
        Field field1 = clazz.getDeclaredField("name");// 私有属性
        field1.setAccessible(true);
        field1.set(person, "张小平");
        Field field2 = clazz.getDeclaredField("age");// 私有属性
        field2.setAccessible(true);
        field2.set(person, 23);
        Field field3 = clazz.getDeclaredField("sex");// 私有属性
        field3.setAccessible(true);
        field3.set(person, "Male");
        Field field4 = clazz.getDeclaredField("isMerried");// 私有属性
        field4.setAccessible(true);
        field4.set(person, true);
        content = person.toString();
        //System.out.println(person.toString());
    }

    public static void invokeSaveFile() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
        Person person = new Person();
        person.setIdNo("5122245566");
        person.setName("张小平");
        person.setAge(23);
        person.setSex("Male");
        person.setMerried(true);

        File file = new File("D:/test.txt");
        if(!file.exists()){
            file.createNewFile();
        }

        byte str[] = new byte[1024];
        String pers = "";
        FileOutputStream in = new FileOutputStream(file);

        Class clazz = Person.class;
        Person person1 = (Person)clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();

        for(Field f : fields){
            String field = (f.toString().substring(f.toString().lastIndexOf(".") + 1));
            pers = field + "=" + f.get(person) + "\n";
            str = pers.getBytes();
            in.write(str, 0, str.length);
        }
    }

    public static void invokeAssignment() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        BufferedReader br = null;
        Class clazz = Class.forName(className);
        Object obj = (Person)clazz.newInstance();
        Field []fields = clazz.getFields();
        try {
            br = new BufferedReader(new FileReader(fileName));

            String str0 = br.readLine();
            String []a0 = str0.split("=");
            fields[0].set(obj, a0[1]);

            String str1 = br.readLine();
            String []a1 = str1.split("=");
            fields[1].set(obj, a1[1]);

            String str2 = br.readLine();
            String []a2 = str2.split("=");
            fields[2].set(obj, Integer.valueOf(a2[1]));

            String str3 = br.readLine();
            String []a3 = str3.split("=");
            fields[3].set(obj, a3[1]);

            boolean isMerried;
            String str4 = br.readLine();
            String []a4 = str4.split("=");
            if(a4[1].equals("true")){
                isMerried = true;
            }else{
                isMerried = false;
            }
            fields[4].set(obj, isMerried);

            //System.out.println(a[1]);
            //fields[i].set(obj, a[1]);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(obj.toString());
    }

    public static void main(String[] args) throws Exception {
        new ThreeDemo().invokeAssignment();
    }
}
